package org.hfy.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import org.hfy.common.ApiResponse;
import org.hfy.dto.SupplierQueryDTO;
import org.hfy.entity.Supplier;
import org.hfy.service.SupplierService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 供应商管理Controller
 * 
 * @author hfy
 * @version 1.0
 */
@RestController
@RequestMapping("/supplier")
@Validated
public class SupplierController {

    @Autowired
    private SupplierService supplierService;

    /**
     * 分页查询供应商列表
     * 
     * @param queryDTO 查询条件
     * @return 分页结果
     */
    @GetMapping("/page")
    public ApiResponse<IPage<Supplier>> getSupplierPage(SupplierQueryDTO queryDTO) {
        try {
            IPage<Supplier> page = supplierService.getSupplierPage(queryDTO);
            return ApiResponse.success(page);
        } catch (Exception e) {
            return ApiResponse.error("查询供应商列表失败：" + e.getMessage());
        }
    }

    /**
     * 根据ID获取供应商详情
     * 
     * @param id 供应商ID
     * @return 供应商信息
     */
    @GetMapping("/{id}")
    public ApiResponse<Supplier> getSupplierById(@PathVariable Long id) {
        try {
            Supplier supplier = supplierService.getSupplierById(id);
            if (supplier == null) {
                return ApiResponse.error(404, "供应商不存在");
            }
            return ApiResponse.success(supplier);
        } catch (Exception e) {
            return ApiResponse.error("获取供应商详情失败：" + e.getMessage());
        }
    }

    /**
     * 新增供应商
     * 
     * @param supplier 供应商信息
     * @return 操作结果
     */
    @PostMapping
    public ApiResponse<String> addSupplier(@Valid @RequestBody Supplier supplier) {
        try {
            boolean success = supplierService.addSupplier(supplier);
            if (success) {
                return ApiResponse.success("新增供应商成功");
            } else {
                return ApiResponse.error("新增供应商失败");
            }
        } catch (Exception e) {
            return ApiResponse.error("新增供应商失败：" + e.getMessage());
        }
    }

    /**
     * 更新供应商
     * 
     * @param id 供应商ID
     * @param supplier 供应商信息
     * @return 操作结果
     */
    @PutMapping("/{id}")
    public ApiResponse<String> updateSupplier(@PathVariable Long id, @Valid @RequestBody Supplier supplier) {
        try {
            supplier.setId(id);
            boolean success = supplierService.updateSupplier(supplier);
            if (success) {
                return ApiResponse.success("更新供应商成功");
            } else {
                return ApiResponse.error("更新供应商失败");
            }
        } catch (Exception e) {
            return ApiResponse.error("更新供应商失败：" + e.getMessage());
        }
    }

    /**
     * 删除供应商
     * 
     * @param id 供应商ID
     * @return 操作结果
     */
    @DeleteMapping("/{id}")
    public ApiResponse<String> deleteSupplier(@PathVariable Long id) {
        try {
            boolean success = supplierService.deleteSupplier(id);
            if (success) {
                return ApiResponse.success("删除供应商成功");
            } else {
                return ApiResponse.error("删除供应商失败");
            }
        } catch (Exception e) {
            return ApiResponse.error("删除供应商失败：" + e.getMessage());
        }
    }

    /**
     * 获取供应商类型选项
     * 
     * @return 供应商类型列表
     */
    @GetMapping("/types")
    public ApiResponse<List<Map<String, String>>> getSupplierTypes() {
        try {
            List<Map<String, String>> types = Arrays.asList(
                new HashMap<String, String>() {{
                    put("value", "material");
                    put("label", "原材料");
                }},
                new HashMap<String, String>() {{
                    put("value", "equipment");
                    put("label", "设备");
                }},
                new HashMap<String, String>() {{
                    put("value", "service");
                    put("label", "服务");
                }},
                new HashMap<String, String>() {{
                    put("value", "other");
                    put("label", "其他");
                }}
            );
            return ApiResponse.success(types);
        } catch (Exception e) {
            return ApiResponse.error("获取供应商类型失败：" + e.getMessage());
        }
    }

    /**
     * 获取状态选项
     * 
     * @return 状态列表
     */
    @GetMapping("/statuses")
    public ApiResponse<List<Map<String, String>>> getSupplierStatuses() {
        try {
            List<Map<String, String>> statuses = Arrays.asList(
                new HashMap<String, String>() {{
                    put("value", "active");
                    put("label", "启用");
                }},
                new HashMap<String, String>() {{
                    put("value", "inactive");
                    put("label", "停用");
                }}
            );
            return ApiResponse.success(statuses);
        } catch (Exception e) {
            return ApiResponse.error("获取状态列表失败：" + e.getMessage());
        }
    }
    
    /**
     * 获取所有供应商列表（不分页）
     * 
     * @return 所有供应商列表
     */
    @GetMapping("/all")
    public ApiResponse<List<Supplier>> getAllSuppliers() {
        try {
            List<Supplier> suppliers = supplierService.getAllSuppliers();
            return ApiResponse.success(suppliers);
        } catch (Exception e) {
            return ApiResponse.error("获取供应商列表失败：" + e.getMessage());
        }
    }
} 